package toubiao.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import toubiao.exception.CustomException;
import toubiao.utils.ProUtilConfig;
import toubiao.utils.ProUtilLogger;
import utils.UtilController;
import utils.UtilString;

@Controller
@RequestMapping("/resource")
public class ResourceController {
	
	private Logger logger=ProUtilLogger.getRootLogger();
	
	private String resourceBasePath=ProUtilConfig.getResourceBasePath();
	

	@RequestMapping("/photo/{photoType}/{fileName}")
	public void imageGetPhoto(HttpServletRequest request,HttpServletResponse response,@PathVariable("photoType")String photoType,@PathVariable("fileName")String fileName) throws CustomException{
		if(UtilString.isNullOrEmpty(photoType,fileName)){
			throw new CustomException("photoType or webPath is null ");
		}else{
			logger.info(fileName);
			logger.info(photoType);
			logger.info(request.getRequestURI());
			
			String uri=request.getRequestURI();
			String suffixOfImageFile=UtilString.getLastSubString(uri, ".");
			fileName=fileName+"."+suffixOfImageFile;
			logger.info(fileName);
			String fileAbsPath=resourceBasePath+"/photo/"+photoType+"/"+fileName;
			UtilController.getImage(fileAbsPath, request, response);
		}
	}
	
	@RequestMapping("/photo/1")
	public void imageGetPhoto1(HttpServletRequest request,HttpServletResponse response,@PathVariable("photoType")String photoType,@PathVariable("webPath")String webPath) throws CustomException{
		logger.info("mapping success.");
	}

	/*@RequestMapping(value = "/file/{webPath}")
	public void getImage(HttpServletRequest request,HttpServletResponse response,@PathVariable("webPath")String webPath) {
		//直接获得webPath 会 将文件的后缀名去掉,如 urL:/file/abc.jpg  webPath=abc,因此需要通过获取uri或者url 获取文件的后缀名
		//logger.info(webPath);
		//logger.info(request.getRequestURI());
		
		String uri=request.getRequestURI();
		String suffixOfImageFile=UtilString.getLastSubString(uri, ".");
		webPath=webPath+"."+suffixOfImageFile;
		logger.info(webPath);
		String fileAbsPath="d:/temp/"+webPath;
		UtilController.getImage(fileAbsPath, request, response);
	}*/
}
